package org.nightrunner.d3.controller.management;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.nightrunner.d3.constant.CRUDConstant;
import org.nightrunner.d3.entity.Reference;
import org.nightrunner.d3.log.SysLog;
import org.nightrunner.d3.model.DefaultPaginationInfo;
import org.nightrunner.d3.model.IPaginationInfo;
import org.nightrunner.d3.model.PaginationModel;
import org.nightrunner.d3.service.IReferenceService;
import org.nightrunner.d3.service.ISearchService;
import org.nightrunner.d3.util.OperateInfo;
import org.nightrunner.d3.util.OperateInfo.Link;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.JsonObject;

/**
 * Reference Management Controller
 * 
 * @author haiyang.chen
 * @date 2012-5-15
 * @version 1.0
 */
@Controller
@RequestMapping("/management/reference/")
public class ReferenceManagementController implements CRUDConstant {

	private static final String RETURN_VIEW_PREFIX = "/management/reference/";

	private static final String LINK_INDEX = "/management/reference/index";

	private static final String LINK_NAME_INDEX = "鸣谢管理首页";

	public static final Logger LOGGER = SysLog.CONTROLLER;

	@Autowired
	private IReferenceService referenceService;

	public IReferenceService getReferenceService() {
		return referenceService;
	}

	public void setReferenceService(IReferenceService referenceService) {
		this.referenceService = referenceService;
	}

	@RequestMapping(value = "list-{currentPage}-{rowSize}", method = RequestMethod.GET)
	public String list(HttpServletResponse response,
			HttpServletRequest request, ModelMap modelMap,
			@PathVariable("rowSize") Integer rowSize,
			@PathVariable("currentPage") Integer currentPage) {
		IPaginationInfo pagination = new DefaultPaginationInfo();
		pagination.setCurrentPage(currentPage);
		pagination.setRowSize(rowSize);

		PaginationModel<List<Reference>> paginationModel = referenceService
				.getReferences(pagination);
		modelMap.addAttribute("paginationModel", paginationModel);
		modelMap.addAttribute("types", types);
		return RETURN_VIEW_PREFIX + VIEW_LIST;
	}

	@RequestMapping(value = "index", method = RequestMethod.GET)
	public String index(HttpServletResponse response,
			HttpServletRequest request, ModelMap modelMap) {
		return list(response, request, modelMap, 10, 0);
	}

	@RequestMapping(value = "", method = RequestMethod.GET)
	public String root(HttpServletResponse response,
			HttpServletRequest request, ModelMap modelMap) {
		return list(response, request, modelMap, 10, 0);
	}

	@RequestMapping(value = "preupdate/{referenceId}", method = RequestMethod.GET)
	public String preUpdate(HttpServletResponse response,
			HttpServletRequest request, ModelMap modelMap,
			@PathVariable("referenceId") String referenceId) {
		Reference reference = referenceService.getReference(referenceId);
		modelMap.addAttribute("reference", reference);
		modelMap.addAttribute("types", types);
		return RETURN_VIEW_PREFIX + VIEW_UPDATE;
	}

	@RequestMapping(value = "add", method = RequestMethod.POST)
	public String add(HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap) {

		Reference reference = new Reference();
		String referenceAuthorName = request
				.getParameter("referenceAuthorName");
		String referenceLink = request.getParameter("referenceLink");
		String referenceType = request.getParameter("referenceType");
		String referenceKeywords = request.getParameter("referenceKeywords");
		String referenceDescription = request
				.getParameter("referenceDescription");

		reference.setReferenceType(referenceType);
		reference.setReferenceLink(referenceLink);
		reference.setReferenceAuthorName(referenceAuthorName);
		reference.setReferenceDescription(referenceDescription);
		reference.setReferenceKeywords(referenceKeywords);

		referenceService.updateReference(reference);

		OperateInfo operateInfo = new OperateInfo();
		operateInfo.setInfo(OPERATE_INFO_UPDATE_OK);
		operateInfo.addLink(new Link(LINK_NAME_INDEX, LINK_INDEX));
		modelMap.addAttribute(REQUEST_KEY_OPERATE_INFO, operateInfo);

		return VIEW_OPERATE_INFO;
	}

	private static final Map<String, String> types = getTypes();

	@RequestMapping(value = "preadd", method = RequestMethod.GET)
	public String preAdd(HttpServletResponse response,
			HttpServletRequest request, ModelMap modelMap) {

		// Reference reference = referenceService.getReference(referenceId);
		//
		// String referenceName = request.getParameter("referenceName");
		// String referenceKeywords = request.getParameter("referenceKeywords");
		// String referenceItemLevel =
		// request.getParameter("referenceItemLevel");
		// String referenceDescription = request
		// .getParameter("referenceDescription");
		//
		// reference.setReferenceName(referenceName);
		// reference.setReferenceDescription(referenceDescription);
		// reference.setReferenceItemLevel(referenceItemLevel);
		// reference.setReferenceKeywords(referenceKeywords);
		//
		// referenceService.updateReference(reference);
		//
		// OperateInfo operateInfo = new OperateInfo();
		// operateInfo.setInfo(OPERATE_INFO_UPDATE_OK);
		// operateInfo.addLink(new Link(LINK_NAME_INDEX, LINK_INDEX));
		// modelMap.addAttribute(REQUEST_KEY_OPERATE_INFO, operateInfo);

		modelMap.addAttribute("types", types);

		return RETURN_VIEW_PREFIX + VIEW_ADD;
	}

	private static Map<String, String> getTypes() {
		Map<String, String> types = new HashMap<String, String>();
		types.put("BUG", "bug");
		types.put("数据", "data");
		types.put("参考", "referece");
		types.put("意见与建议", "idea");
		return types;
	}

	@RequestMapping(value = "update/{referenceId}", method = RequestMethod.POST)
	public String update(HttpServletResponse response,
			HttpServletRequest request, ModelMap modelMap,
			@PathVariable("referenceId") String referenceId) {

		Reference reference = referenceService.getReference(referenceId);
		String referenceAuthorName = request
				.getParameter("referenceAuthorName");
		String referenceLink = request.getParameter("referenceLink");
		String referenceType = request.getParameter("referenceType");
		String referenceKeywords = request.getParameter("referenceKeywords");
		String referenceDescription = request
				.getParameter("referenceDescription");

		reference.setReferenceType(referenceType);
		reference.setReferenceLink(referenceLink);
		reference.setReferenceAuthorName(referenceAuthorName);
		reference.setReferenceDescription(referenceDescription);
		reference.setReferenceKeywords(referenceKeywords);

		referenceService.updateReference(reference);

		OperateInfo operateInfo = new OperateInfo();
		operateInfo.setInfo(OPERATE_INFO_UPDATE_OK);
		operateInfo.addLink(new Link(LINK_NAME_INDEX, LINK_INDEX));
		modelMap.addAttribute(REQUEST_KEY_OPERATE_INFO, operateInfo);

		return VIEW_OPERATE_INFO;
	}

	@Autowired
	private ISearchService searchService;

	public ISearchService getSearchService() {
		return searchService;
	}

	public void setSearchService(ISearchService searchService) {
		this.searchService = searchService;
	}

	// @RequestMapping(value = "search/{keyword}-{currentPage}-{rowSize}",
	// method = RequestMethod.GET)
	// public String result(ModelMap modelMap,
	// @PathVariable("keyword") String keyword,
	// @PathVariable("currentPage") Integer currentPage,
	// @PathVariable("rowSize") Integer rowSize) throws Exception {
	// if (keyword == null || keyword.trim().length() == 0) {
	// return "redirect:/" + RETURN_VIEW_PREFIX + VIEW_INDEX;
	// }
	//
	// IPaginationInfo pagination = new DefaultPaginationInfo();
	// pagination.setCurrentPage(currentPage);
	// pagination.setRowSize(rowSize);
	//
	// List<String> keywords = new ArrayList<String>();
	// keywords.add(keyword);
	//
	// PaginationModel<List<Reference>> paginationModel = searchService
	// .searchReferences(keywords, pagination);
	// modelMap.addAttribute("keyword", keyword);
	//
	// modelMap.addAttribute("paginationModel", paginationModel);
	//
	// return RETURN_VIEW_PREFIX + "search/" + VIEW_LIST;
	// }

	@RequestMapping(value = "delete/{referenceId}", method = {
			RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public String delete(@PathVariable("referenceId") String referenceId,
			HttpServletResponse response, ModelMap modelMap) {
		JsonObject object = new JsonObject();
		Reference reference = referenceService.getReference(referenceId);

		if (reference == null) {
			object.addProperty(CRUD_SUCCESS, false);
			object.addProperty(CRUD_INFO, "事项不存在");
		} else {
			try {
				referenceService.deleteReference(reference);
				object.addProperty(CRUD_SUCCESS, true);
			} catch (Exception ex) {
				object.addProperty(CRUD_SUCCESS, false);
			}
		}

		return object.toString();
	}
}
